<?php
class AlbumsController extends AppController {

	var $name = 'Albums';
	var $uses = array('Album','Cuisine');

	var $paginate = array(
			  'limit' => 6,
			  'order' => array(
			  	'Album.is_featured' => 'desc',
				'Album.id' => 'desc'
				)
			  );  

	function index () {
		
		//$data = $this->Album->find('all');
		$data = $this->paginate('Album');
		//pr($data);die();
		$this->set('title_for_layout','Albums');
		$this->set('data', $data);
		
	}

	function cuisines ($id = null) {

		$this->paginate = array(
			  'limit' => 20,
			  'conditions' => array('Album.cuisine_id' => $id),
			  'order' => array(
				'Album.is_featured' => 'desc',
				'Album.id' => 'desc'
				)
			  );  

		
		
		if (isset($this->params['pass'][1])) {
			//pr($this->params['pass']);
			$listof = $this->params['pass'][1];
			if ($listof == 'featured') {
				$this->paginate['conditions'] = array('Album.cuisine_id'=> $id, 'Album.is_featured'=>1);
			}

			if ($listof == 'popular') {
				$this->paginate['conditions'] = array('Album.cuisine_id'=> $id, 'Album.hits >'=>50);
			}

		} 
		
		$data = $this->paginate('Album');
		$cuisines = $this->Cuisine->find('first',array('conditions'=>array('Cuisine.id'=>$id)));
		$this->set('cuisine',$cuisines);
		$this->set('title_for_layout',$cuisines['Cuisine']['title']);
		//pr($data);die();
		
		$this->set('data', $data);
		
		$this->set('page_background',$cuisines['Cuisine']['path']);
	}
	
	function category ($id = null) {
		$directory = getcwd().DS.'img'.DS.'albums'.DS.$id.DS;
		
		$images = glob($directory . "*.jpg");
		
		foreach($images as $image) {
			$data['Image'][]['path'] = $id.'/'.str_replace($directory,'',$image);
		}

		$album = $this->Album->find('first',array('conditions'=>array('Album.id'=>$id)));
		$cuisine = $this->Cuisine->find('first',array('conditions'=>array('Cuisine.id'=>$album['Album']['cuisine_id'])));

		$data['Album'] = $album['Album'];
		$data['Cuisine'] = $cuisine['Cuisine'];
		$this->Album->addHits($id);
		$this->set('title_for_layout',$album['Album']['title']);
		$this->set('data',$data);

	}

	function featured() {
		
	}
	
	function add() {
		
		
		if ($data = $this->data) {
		
			if ($this->Album->save($data)) {
				$lastId = $this->Album->getLastInsertID();
				$this->Session->setFlash('Album was successfully save. Album ID: '.$lastId);
				$this->redirect(array('action'=>'add'));
			}

		}
		
		$this->set('dishes',$this->Album->find('list'));
		$this->set('cuisines',$this->Cuisine->find('list'));
	}
	
	function previewAlbum() {
		
		if ($this->RequestHandler->isAjax()) {
			$data = $this->Album->find('first');
			pr($data);
		}
		
		die(); 
	}

}
?>